package com.taxshare.jwt;

import org.apache.commons.codec.binary.Base64;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import javax.crypto.Cipher;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.security.Key;

public class RsaKeyHelper {

    /**
     * 指定加密算法为RSA
     */
    private static final String ALGORITHM = "RSA";

    /**
     * 指定公钥存放文件
     */
    public static final String PUBLIC_KEY_FILE = "PublicKey";

    /**
     * 指定私钥存放文件
     */
    public static final String PRIVATE_KEY_FILE = "PrivateKey";

    public static Key publicKey = null;

    public static Key privateKey = null;

    /**
     * 加密方法
     *
     * @param source 源数据
     */
    public static String encrypt(String source) throws Exception {
        if (publicKey == null) {
            publicKey = getKey(PUBLIC_KEY_FILE);
        }
        /** 得到Cipher对象来实现对源数据的RSA加密 */
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] b = source.getBytes();
        /** 执行加密操作 */
        byte[] b1 = cipher.doFinal(b);
        Base64 base64 = new Base64();
        return new String(base64.encode(b1));
    }

    /**
     * 解密算法
     *
     * @param cryptograph 密文
     */
    public static String decrypt(String cryptograph) throws Exception {
        if (privateKey == null) {
            privateKey = getKey(PRIVATE_KEY_FILE);
        }
        /** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        Base64 base64 = new Base64();
        byte[] b1 = base64.decode(cryptograph.getBytes());

        /** 执行解密操作 */
        byte[] b = cipher.doFinal(b1);
        return new String(b);
    }

    public static Key getKey(String keyType) throws Exception {
        if (keyType.equals(PUBLIC_KEY_FILE)) {
            if (publicKey == null) {
                return LoadKeyFromFile(PUBLIC_KEY_FILE);
            } else {
                return publicKey;
            }
        }
        if (keyType.equals(PRIVATE_KEY_FILE)) {
            if (privateKey == null) {
                return LoadKeyFromFile(PRIVATE_KEY_FILE);
            } else {
                return privateKey;
            }
        }
        return null;

    }

    private static Key LoadKeyFromFile(String fileName) throws Exception {
        Key key;
        ObjectInputStream ois = null;
        InputStream is = null;
        try {
            //将文件中的私钥对象读出
            Resource resource = new ClassPathResource("rsa-config/" + fileName);
            ois = new ObjectInputStream(resource.getInputStream());
            key = (Key) ois.readObject();
        } catch (Exception e) {
            throw e;
        } finally {
            if (is != null) {
                is.close();
            }
            if (ois != null) {
                ois.close();
            }
        }
        return key;
    }

    private static Key LoadKeyFromFileByJar(String fileName) throws Exception {
        ObjectInputStream ois = null;
        InputStream is = null;
        Key key;
        try {
            is = RsaKeyHelper.class.getClassLoader().getResourceAsStream("rsa-config/" + fileName);
            ois = new ObjectInputStream(is);
            key = (Key) ois.readObject();
        } catch (Exception var8) {
            throw var8;
        } finally {
            if (is != null) {
                is.close();
            }
            if (ois != null) {
                ois.close();
            }
        }
        return key;
    }

    // public static void main(String[] args) throws Exception{
    //     System.out.println(LoadKeyFromFile("PublicKey"));
    //     System.out.println(LoadKeyFromFileByJar("PublicKey"));
    // }

}
